<?php

/* * ****************************************************************************
 * controllers\RepairController
 * 
 * author  Laureta Xhaferaj
 * email   laureta@first.al
 * 
 * @function create
 * @function create_repair
 * @function destroy_repair
 * @function destroy_repairPart
 * @function edit_repair
 * @function edite_repairParts
 * @function index
 * @function index_repairParts
 * @function show
 * @function store
 * @function store_repairParts
 * @function update_repair
 * @function update_repairParts
 * @function vehicle_repair
 * *************************************************************************** */

class RepairController extends \BaseController {

    /**
     * Display all repairs.
     *
     * @return array
     */
    public function index($id) {
        $repairs = DB::select('select r.*, r.id as repair_id, u.first_name, s.*,
            c.Make,c.Model, c.id, c.status, c.License,p.repair,p.id from repairs as r
                                left join users as u on u.id = r.user_id
                                left join services as s on s.id = r.service_id
                                left join cars as c on c.id = r.car_id
                                left join repair_parts as p on p.id=r.repair_id
                                where r.car_id = ?', array($id));
        return View::make('pages.repair', array(
                    'repairs' => $repairs,
                    'car_id' => $id,
                    'make' => $repairs[0]->Make,
                    'model' => $repairs[0]->Model,
        ));
    }

    /**
     * The form to create a new repair.
     *
     * @return array
     */
    public function create($id) {
        $repairs = DB::table('repair_parts')->orderBy('repair', 'asc')->distinct()->lists('repair', 'id');
        array_walk($repairs, function(&$value, $key) {
            $value = Lang::get('repair.' . $value);
        });
        $services = DB::table('services')->orderBy('name', 'asc')->distinct()
                ->lists('name', 'id');
        $currencies = array(
            'EUR' => 'EUR',
            'LEK' => 'LEK',
        );
        return View::make('pages.repair_create', array(
                    'repairs' => $repairs,
                    'services' => $services,
                    'car_id' => $id,
                    'currencies' => $currencies
        ));
    }

    public function repair_problem($id) {
        $data = explode('~', $id);
        $vehicle = $data[0];
        $type = $data[1];
        $problem_id = $data[2];

        $currencies = array(
             'EUR' => 'EUR',
            'LEK' => 'LEK',
        );
        $services = DB::table('services')->orderBy('name', 'asc')->distinct()
                ->lists('name', 'id');

        return View::make('pages.repair_problem', array(
                    'vehicle_id' => $vehicle,
                    'type' => $type,
                    'problem_id' => $problem_id,
                    'currencies' => $currencies,
                    'services' => $services
        ));
    }

    public function repair_prob_store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'repair' => Lang::get('messages.repair'),
            'repair_date' => Lang::get('messages.repair_date'),
            'price' => Lang::get('messages.price'),
            'service' => Lang::get('messages.service'),
        );
        $rules = array(
            "repair" => "required",
            "repair_date" => "required",
            "price" => "required",
            "service" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $car_id = Input::get('vehcile_id');
            $problem_id = Input::get('problem_id');
            $repair = new RepairPart;
            $repair->repair = Input::get('repair');
            $repair->user_id = Auth::user()->id;
            $repair->save();
            $data = new Repair;
            $data->repair_id = $repair->id;
            $data->repair_date = Input::get('repair_date');
            $data->price = Input::get('price');
            $data->service_id = Input::get('service');
            $data->nr_invoice = Input::get('nr_invoice');
            $data->comment = Input::get('comment');
            $data->car_id = Input::get('vehicle_id');
            $data->currency = Input::get('currency');
            $data->user_id = Auth::user()->id;
            $data->save();

            $values = new RepairsInsert;
            $values->repair_id = $repair->id;
            $values->repair_date = Input::get('repair_date');
            $values->price = Input::get('price');
            $values->service_id = Input::get('service');
            $values->nr_invoice = Input::get('nr_invoice');
            $values->comment = Input::get('comment');
            $values->car_id = Input::get('vehicle_id');
            $values->currency = Input::get('currency');
            $values->user_id = Auth::user()->id;
            $values->save();

            $data2 = Problem::find($problem_id);
            $data2->status_id = 1;
            $data2->save();

            return Response::json(["redirect_to" => "../../problems/".Input::get('vehicle_id')]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            }
        }
    }

    /**
     * Store new repair data.
     *
     * @return Response
     */
    public function store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'repair' => Lang::get('messages.repair'),
            'repair_date' => Lang::get('messages.repair_date'),
            'price' => Lang::get('messages.price'),
            'service' => Lang::get('messages.service'),
        );
        $rules = array(
            "repair" => "required",
            "repair_date" => "required",
            "price" => "required",
            "service" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $car_id = Input::get('car_id');
            $value = Input::get('other_repair');
            // redirect
            if (!empty($value)) {
                $repair = new RepairPart;
                $repair->repair = Input::get('other_repair');
                //$repair->status_id = 2;
                $repair->user_id = Auth::user()->id;
                $repair->save();

                $data = new Repair;
                $data->repair_id = $repair->id;
                $data->repair_date = Input::get('repair_date');
                $data->price = Input::get('price');
                $data->service_id = Input::get('service');
                $data->nr_invoice = Input::get('nr_invoice');
                $data->comment = Input::get('comment');
                $data->car_id = Input::get('car_id');
                $data->currency = Input::get('currency');
                $data->user_id = Auth::user()->id;
                $data->save();

                $values = new RepairsInsert;
                $values->repair_id = $repair->id;
                $values->repair_date = Input::get('repair_date');
                $values->price = Input::get('price');
                $values->service_id = Input::get('service');
                $values->nr_invoice = Input::get('nr_invoice');
                $values->comment = Input::get('comment');
                $values->car_id = Input::get('car_id');
                $values->currency = Input::get('currency');
                $values->user_id = Auth::user()->id;
                $values->save();
            } else {
                $data = new Repair;
                $data->repair_id = Input::get('repair');
                $data->repair_date = Input::get('repair_date');
                $data->price = Input::get('price');
                $data->service_id = Input::get('service');
                $data->nr_invoice = Input::get('nr_invoice');
                $data->comment = Input::get('comment');
                $data->car_id = Input::get('car_id');
                $data->currency = Input::get('currency');
                $data->user_id = Auth::user()->id;
                $data->save();

                $values = new RepairsInsert;
                $values->repair_id = Input::get('repair');
                $values->repair_date = Input::get('repair_date');
                $values->price = Input::get('price');
                $values->service_id = Input::get('service');
                $values->nr_invoice = Input::get('nr_invoice');
                $values->comment = Input::get('comment');
                $values->car_id = Input::get('car_id');
                $values->currency = Input::get('currency');
                $values->user_id = Auth::user()->id;
                $values->save();
            }
            return Response::json(["redirect_to" => "../../repair/$car_id"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        
    }

    /**
     * The form for edit repair parts.
     *
     * @param  int  $id
     * @return array
     */
    public function edit_repairParts($id) {
        $values = DB::select('select * from repair_parts where id=?', array($id));
        return View::make('pages.repair_parts_edit', array(
                    'values' => $values
        ));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update_repairParts($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'repair' => Lang::get('messages.repair'),
        );
        $rules = array(
            "repair" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $data = RepairPart::find($id);
            $data->repair = Input::get('repair');
            $data->save();
            return Response::json(["redirect_to" => "../../all_repairs"]);
        } elseif (Request::ajax()) {
            $response_values = array(
                'validation_failed' => 1,
                'errors' => $validation->errors()->toArray(),
            );
            return Response::json($response_values);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit_repair($id) {
        $values = DB::select('select r.*,r.id as repairId,s.*,c.id,p.* from repairs as r '
                        . 'left join services as s on s.id=r.service_id '
                        . 'left join cars as c on c.id=r.car_id '
                        . 'left join repair_parts as p on p.id=r.repair_id '
                        . 'where r.id=?', array($id));
        $repairs = DB::table('repair_parts')->orderBy('repair', 'asc')->distinct()->lists('repair', 'id');
        //  ->where('status_id', '1')
        array_walk($repairs, function(&$value, $key) {
            $value = Lang::get('repair.' . $value);
        });
        $services = DB::table('services')->orderBy('name', 'asc')->distinct()
                ->lists('name', 'id');
        $currencies = array(
             'EUR' => 'EUR',
            'LEK' => 'LEK',
        );
        return View::make('pages.repair_edit', array(
                    'values' => $values,
                    'id' => $id,
                    'repairs' => $repairs,
                    'services' => $services,
                    'currencies' => $currencies
        ));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update_repair($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'repair' => Lang::get('messages.repair'),
            'price' => Lang::get('messages.price'),
            'repair_date' => Lang::get('messages.repair_date'),
            'service' => Lang::get('messages.service'),
        );
        $rules = array(
            "repair" => "required",
            "price" => "required",
            "repair_date" => "required",
            "service" => "required",
        );
        $car_id = DB::select('select car_id from repairs where id=?', array($id));
        $car = $car_id[0]->car_id;
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $repair = Repair::find($id);
            $repair->repair_id = Input::get('repair');
            $repair->price = Input::get('price');
            $repair->repair_date = Input::get('repair_date');
            $repair->service_id = Input::get('service');
            $repair->nr_invoice = Input::get('nr_invoice');
            $repair->currency = Input::get('currency');
            $repair->comment = Input::get('comment');
            $repair->save();
            return Response::json(["redirect_to" => "../../repair/" . $car]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                echo 'error';
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy_repair($id) {
        $car = DB::select('select car_id from repairs where id=?', array($id));
        $repair_data = DB::select('select * from repairs where id=?', array($id));

        $repair = Repair::find($id);

        $data = new RepairsDelete;
        $data->repair_id = $repair_data[0]->repair_id;
        $data->repair_date = $repair_data[0]->repair_date;
        $data->price = $repair_data[0]->price;
        $data->service_id = $repair_data[0]->service_id;
        $data->nr_invoice = $repair_data[0]->nr_invoice;
        $data->comment = $repair_data[0]->comment;
        $data->car_id = $repair_data[0]->car_id;
        $data->user_id = Auth::user()->id;
        $data->save();

        $repair->delete();
        return Redirect::to('repair/' . $car[0]->car_id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy_repairPart($id) {
        $repair_data = DB::select('select * from repair_parts where id=?', array($id));
        $repair = RepairPart::find($id);

        $data = New RepairPartsDelete();

        $data->repair = $repair_data[0]->repair;
        $data->desc = $repair_data[0]->desc;
        $data->user_id = Auth::user()->id;
        $data->save();

        $repair->delete();
        return Redirect::to('repair_parts');
    }

    public function index_repairParts() {
        $all_repairs = DB::select('select o.*,u.Username from repair_parts as o '
                        . 'left join users as u on u.id=o.user_id ');
        return View::make('pages.repair_parts', array(
                    'all_repairs' => $all_repairs,
        ));
    }

    public function store_repairParts() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'repair_parts' => Lang::get('messages.repair'),
        );
        $rules = array(
            "repair_parts" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $data = new RepairPart;
            $data->repair = Input::get('repair_parts');
            $data->desc = Input::get('desc');
            $data->user_id = Auth::user()->id;
            //$data->status_id = 1;
            $data->save();

            $parts = new RepairpartsInsert;
            $parts->repair = Input::get('repair_parts');
            $parts->desc = Input::get('desc');
            $parts->user_id = Auth::user()->id;
            //$parts->status_id = 1;
            $parts->save();

            //redirect
            return Response::json(["redirect_to" => "repair_parts"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            }
        }
    }

    public function create_repairPart() {
        return View::make('pages.repairParts_create');
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function vehicle_repair() {
        $vehicle_repair = DB::select('SELECT r. * , u.username, c.Model, 
            c.Make, c.License, c.Milage, p.repair, COUNT( r.repair_id )
            AS count, MAX(r.repair_date) as max_date
                    FROM repairs AS r
                    LEFT JOIN
                         users AS u
                         ON u.id = r.user_id 
                    LEFT JOIN
                         cars AS c
                         ON c.id = r.car_id
                         LEFT JOIN
                         repair_parts as p 
                         ON p.id = r.repair_id
                    GROUP BY car_id');
        return View::make('pages.vehicle_repair', array(
                    'vehicle_repair' => $vehicle_repair,
        ));
    }

    public function problems($id) {
        $values = DB::select('select p.*,c.Make, c.Model from problems as p '
                        . 'left join cars as c on c.id=p.vehicle_id '
                        . 'where vehicle_id=?', array($id));

        return View::make('pages.problems', array(
                    'values' => $values
        ));
    }

    public function problems_create() {
        $cars = DB::table('cars')->orderBy('id', 'asc')
                        ->distinct()->lists('Make', 'id');
        $status = array(
            0 => 'Problem',
            1 => 'Repaired',
        );

        return View::make('pages.problems_create', array(
                    'cars' => $cars,
                    'status' => $status
        ));
    }

    public function problems_store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'type' => Lang::get('messages.type'),
            'status' => Lang::get('messages.status'),
        );
        $rules = array(
            "type" => "required",
            "status" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $vehicle_id=Input::get('vehicle');
            $data = new Problem;
            $data->vehicle_id = Input::get('vehicle');
            $data->type = Input::get('type');
            $data->status_id = Input::get('status');
            $data->comment = Input::get('comment');
            $data->user_id = Auth::user()->id;
            $data->save();

            return Response::json(["redirect_to" => "problems/".$vehicle_id]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            }
        }
    }

}
